<template>
    <div class="container">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="题库名称" prop="p1">
                <el-input type="text" v-model="ruleForm2.p1" auto-complete="off"></el-input>
            </el-form-item>
            <!--<el-form-item label="排序" prop="p2">-->
                <!--<el-input type="number" v-model="ruleForm2.p2" auto-complete="off"></el-input>-->
            <!--</el-form-item>-->
            <!--<el-form-item label="描述" prop="p2">-->
                <!--<el-input type="text" v-model="ruleForm2.p2" auto-complete="off"></el-input>-->
            <!--</el-form-item>-->

            <!--<el-form-item label="权限">-->
                <!--<el-tree-->
                    <!--:data="tree"-->
                    <!--show-checkbox-->
                    <!--default-expand-all-->
                    <!--node-key="id"-->
                    <!--ref="tree"-->
                    <!--highlight-current-->
                    <!--:props="defaultProps">-->
                <!--</el-tree>-->
            <!--</el-form-item>-->

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
                <!--<el-button @click="getCheckedNodes">获取</el-button>-->
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import pub from '../../utils/pub'

    export default {
        data: function () {
            var v1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入题库名称(2-20字)'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-20位'));
                } else {
                    callback();
                }
            };
            var v2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入题库排序'));
                }else {
                    callback();
                }
            };
            return {
                tree: [],
                defaultProps: {
                    children: 'children',
                    label: 'name'
                },
                arr: [],
                ruleForm2: {
                    p1: '',
                    p2: '',
                },
                rules2: {
                    p1: [
                        {required: true, validator: v1, trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, validator: v2, trigger: 'blur'}
                    ],
                }
            }
        },
        components: {
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({
                    lock: true,
                    text: 'Loading',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.2)'
                });
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {
                this.loading.close();
            },
            // 获取角色信息
            getInfo(qus) {
                // console.log(this.uid)
                if(!this.uid) {
                    this.ruleForm2.p1 = '';
                    this.ruleForm2.p2 = '';
                    return;
                }
                if(this.uid){
                    this.ruleForm2.p1 = qus.question_bank_name;
                    // this.ruleForm2.p2 = qus.sorts;
                }
                // this.loadopen();
                // this.config.get('/supply/role/findById', {id: this.uid}).then(res => {
                //     this.loadclose();
                //     if (res.data.code === '0') {
                //         // console.log(res.data, '获取角色信息');
                //         this.ruleForm2.p1 = res.data.supplyRole.name;
                //         this.ruleForm2.p2 = res.data.supplyRole.remark;
                //         this.setCheckedKeys(res.data.supplyRole.ids.split(',') || []);
                //     } else {
                //         this.$message.error(this.config.handleError(res.data));
                //     }
                // });
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'questionBank.id': this.uid,
                            'questionBank.question_bank_name': this.ruleForm2.p1,
                            // 'qb.sorts': this.ruleForm2.p2,
                        };
                        //console.log(obj);
                        let url = this.uid ? '/admin/questionbank/update' : '/admin/questionbank/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
